package com.example.viewtest.AdapterGroup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.viewtest.Dao.Bean;
import com.example.viewtest.R;

import java.util.List;

public class Adapter_FragmentThree extends RecyclerView.Adapter {

    public static final int FT_HEAD=0;
    public static final int FT_ONE = 1;
    public static final int FT_TWO = 2;

    private List<Bean> mdata;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    //头部View个数
    private final int mHeaderCount=1;

    public Adapter_FragmentThree(){}

    public Adapter_FragmentThree(Context context,List<Bean> list){
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mdata = list;
    }

    public boolean isHeaderView(int position) {
        return position < mHeaderCount;
    }

    public static class HeadFragmentThreeHolder extends RecyclerView.ViewHolder{
        public HeadFragmentThreeHolder(View item){
            super(item);
        }
    }

    public static class OneHolderFragmentThree extends RecyclerView.ViewHolder{
        private ImageView imageView;
        public OneHolderFragmentThree(View item){
            //会把item赋值给RecyclerView的itemView，所以用本身的item和用RecyclerView的itemView本质相同
            super(item);
            imageView = itemView.findViewById(R.id.fragmentThree_One_item_image);
        }
    }

    public static class TwoHolderFragmentThree extends RecyclerView.ViewHolder{
        private ImageView imageView1;
        private ImageView imageView2;
        public TwoHolderFragmentThree(View item){
            super(item);
            imageView1 = itemView.findViewById(R.id.fragmentThree_Two_item_image1);
            imageView2 = itemView.findViewById(R.id.fragmentThree_Two_item_image2);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == FT_HEAD){
            return FT_HEAD;
        }else if (position%2==0){
            return FT_TWO;
        }else{
            return FT_ONE;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == FT_HEAD){
            return new HeadFragmentThreeHolder(mLayoutInflater.inflate(R.layout.fragment_three_header,parent,false));
        }else if (viewType == FT_ONE){
            return new OneHolderFragmentThree(mLayoutInflater.inflate(R.layout.fragment_three_view_one,parent,false));
        }else{
            return new TwoHolderFragmentThree(mLayoutInflater.inflate(R.layout.fragment_three_view_two,parent,false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadFragmentThreeHolder){

        }else if (holder instanceof OneHolderFragmentThree){
            ((OneHolderFragmentThree) holder).imageView.setImageResource(mdata.get(position).getImageId());
        }else{
            Glide.with(mContext)
                    .load(mdata.get(position).getImageId())
                    .into(((TwoHolderFragmentThree) holder).imageView1);
            Glide.with(mContext)
                    .load(mdata.get(position).getImageId())
                    .into(((TwoHolderFragmentThree) holder).imageView2);
        }
    }

    @Override
    public int getItemCount() {
        return mdata.size() + mHeaderCount;
    }
}
